package exam.controller;


import com.google.code.kaptcha.Producer;
import exam.bean.UserInfo;
import exam.service.IPatentTypeService;
import exam.service.IUserInfoService;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class AuthController extends BaseController{

    @Autowired
    Producer producer;

    @Autowired
    IUserInfoService userInfoService;

    @Autowired
    IPatentTypeService patentTypeService;

    @GetMapping("/captcha")
    public Msg captcha() throws IOException {
        String key = UUID.randomUUID().toString();
        String code = producer.createText();

        BufferedImage image = producer.createImage(code);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image,"jpg",outputStream);

        BASE64Encoder encoder = new BASE64Encoder();
        String str = "data:image/jpg;base64,";

        String base64Image = str + encoder.encode(outputStream.toByteArray());

        redisUtil.hset("captcha",key,code,200);

        return Msg.success().add("key",key).add("base64Img",base64Image);
    }

    @GetMapping("/userInfo")
    public Msg userInfo(Principal principal){
        //查询登录用户
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("name",principal.getName());
        UserInfo userInfo = userInfoService.selectInfoSingle(queryMap);
        return Msg.success().add("userName",userInfo.getName()).add("userId",userInfo.getId());
    }

    @GetMapping("/allPermission")
    public Msg allPermission(){
        return Msg.success().add("menu",groupAndMenuInfoService.allPermissionMenuInfo());
    }


    @GetMapping("/patentTypeList")
    public Msg patentTypeList(){
        return Msg.success().add("patentType",patentTypeService.getPatentTypeList());
    }

//    用户注册
    @PostMapping("/register")
    public Msg register(@RequestBody UserInfo userInfo){
        //用户注册默认字段设置
        userInfo.setFlag(0);
        userInfo.setUserGroupId(7);   //权限默认分组
        int re = userInfoService.insertInfo(userInfo);
        return re > 0 ?  Msg.success().setMessage("注册成功") : Msg.fail().setMessage("注册失败");
    }


    /**
     * 检查用户是否存在
     */
    @GetMapping("/theUserIsExit")
    public Msg userIsExist(@RequestParam(value = "userName",defaultValue = "") String userName){
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("name",userName);
        UserInfo userInfo =  userInfoService.selectInfoSingle(queryMap);
        if(userInfo == null){
            return Msg.fail().setMessage("用户不存在");
        }else{
            return Msg.success().setMessage("用户已存在");
        }


    }
}
